package ie.dkit.semanticPhotoWeb.client.panels.album;

import ie.dkit.semanticPhotoWeb.client.ClientServerConnectorAsync;
import ie.dkit.semanticPhotoWeb.client.panels.CenterPanel;
import ie.dkit.semanticPhotoWeb.client.tools.AsyncCallbackAdapter;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextArea;
import com.gwtext.client.widgets.form.TextField;

/**
 * @author Sebastian Ruehl
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
public class AddAlbumWindow extends Window {

	private final ClientServerConnectorAsync m_ClientServerConnectorAsync;

	private final CenterPanel m_CenterPanel;

	private final TextField m_AlbumTitle;

	private final TextArea m_Description;

	private final FormPanel m_FormPanel;

	private final Button m_SaveButton;
	
	private final Button m_CancelButton;

	public AddAlbumWindow(
			final ClientServerConnectorAsync clientServerConnectorAsync,
			final CenterPanel centerPanel) {
		super("Add a new Album", true, true);
		// Fix due issues..
		setSize(260, 150);
		m_ClientServerConnectorAsync = clientServerConnectorAsync;
		m_CenterPanel = centerPanel;
		setCloseAction(Window.CLOSE);

		m_FormPanel = new FormPanel();
		m_FormPanel.setMonitorValid(true);

		m_AlbumTitle = new TextField("Album Title");
		m_AlbumTitle.setAllowBlank(false);
		m_FormPanel.add(m_AlbumTitle);

		m_Description = new TextArea("Description");
		m_FormPanel.add(m_Description);

		m_SaveButton = new Button("Create", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				if (m_AlbumTitle.isValid() && m_Description.isValid()) {
					MessageBox.wait("Creating Album");
					m_ClientServerConnectorAsync.newAlbum(m_AlbumTitle
							.getText(), m_Description.getText(),
							new AsyncCallbackAdapter() {
								public void onSuccess(Object result) {
									MessageBox.alert("Successfully created");
									m_CenterPanel.refresh();
									AddAlbumWindow.this.close();
								}
							});
				} else {
					MessageBox.alert("Error", "Please Check you input");
				}

			}
		});
		m_FormPanel.addButton(m_SaveButton);

		m_CancelButton = new Button("Cancel", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				AddAlbumWindow.this.close();
			}
		});
		m_FormPanel.addButton(m_CancelButton);

		add(m_FormPanel);
	}
}
